/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scraper.dao;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import org.jaxen.jdom.JDOMXPath;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import scraper.DataLocator;
import scraper.ScraperException;

/**
 *
 * @author thedaego
 */
public class Data{

    private String name;
    private String source;
    public String value;


    public Data(String name, String value, String source) {
        this.name = name;
        this.source = source;
        this.value = value;
    }

    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
/**
     * loads the targeted data as specified in the DataLocator into this object's
     * value field
     * @param locator
     * @throws ScraperException 
     */    
    public static Data load(DataLocator locator) throws ScraperException {
        String targetData = null;
        try {
            HttpURLConnection conn = (HttpURLConnection) locator.getLocatorUrl().openConnection();
            //set the user agent in order to bypas 403 errors
            conn.addRequestProperty("User-Agent", "Mozilla/5.0 "
                    + "(Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) "
                    + "Gecko/20061204 Firefox/2.0.0.1");
            // build a JDOM tree from a SAX stream provided by tagsoup
            SAXBuilder builder = new SAXBuilder("org.ccil.cowan.tagsoup.Parser");
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            Document doc;
  

            doc = builder.build(reader);

            JDOMXPath dataPath = new JDOMXPath(locator.getXpath());
            dataPath.addNamespace("h","http://www.w3.org/1999/xhtml");
 
            targetData = ((Element)dataPath.selectSingleNode(doc)).getText();

        } catch (Exception ex) {
            throw new ScraperException("Error retrieving data from: "
                    + locator.getLocatorUrl().getFile()
                    + "\n" +ex.getMessage(), ex);
        }

        Data returnData = new Data(locator.getLocatorName(), 
                targetData, locator.getLocatorUrl().getPath());
        return returnData;
        
    }
    

    
}
